package com.pany.rememberpass;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    public static final int RESULT_CODE_REGISTER = 0;
    private CheckBox cbx_Agree;
    private Button btn_register;
    private EditText et_phone, et_pass, et_pass_two;
    private SharedPreferences spfRecorid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        getSupportActionBar().setTitle("注册");

        spfRecorid = getSharedPreferences("spfRecorid", MODE_PRIVATE);

        cbx_Agree = findViewById(R.id.cbx_Agree);
        btn_register = findViewById(R.id.btn_register);
        et_phone = findViewById(R.id.et_phone);
        et_pass = findViewById(R.id.et_pass);
        et_pass_two = findViewById(R.id.et_pass_two);

        btn_register.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        String strPhone = et_phone.getText().toString();
        String strPass = et_pass.getText().toString();
        String strPassTwo = et_pass_two.getText().toString();


        if (TextUtils.isEmpty(strPhone)) {
            Toast.makeText(RegisterActivity.this, "账号不能为空", Toast.LENGTH_SHORT).show();
        } else if (TextUtils.isEmpty(strPass)) {
            Toast.makeText(RegisterActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
        } else if (TextUtils.isEmpty(strPassTwo)) {
            Toast.makeText(RegisterActivity.this, "确认密码不能为空", Toast.LENGTH_SHORT).show();
        }

        if (!TextUtils.equals(strPass, strPassTwo)) {

            Toast.makeText(RegisterActivity.this, "密码不一致", Toast.LENGTH_SHORT).show();
        } else {

            if (cbx_Agree.isChecked()) {
                // 存储账号密码
                SharedPreferences.Editor editor = spfRecorid.edit();
                editor.putString("phone", strPhone);
                editor.putString("passWord", strPass);
                editor.apply();
                Toast.makeText(RegisterActivity.this, "注册成功了", Toast.LENGTH_SHORT).show();

                Bundle bundle = new Bundle();
                bundle.putString("phone", strPhone);
                bundle.putString("passWord", strPass);

                Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
                intent.putExtras(bundle);
                setResult(RESULT_CODE_REGISTER,intent);

                RegisterActivity.this.finish();
            } else {
                Toast.makeText(RegisterActivity.this, "请同意用户协议", Toast.LENGTH_SHORT).show();
            }

        }
    }


}